<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\AgreementModel;
use app\model\RedisModel;
use think\App;

class Agreement extends Base {

    protected $middleware = [Auth::class];
    private $agreementModel;

    public $cacheKey="agreement:id:";

    private $req;


    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 999999;

        $this->agreementModel = new AgreementModel();
    }


    /*
     * 获取店铺列表
     * */
    public function getInfo(){

        $info = $this->agreementModel->getInfo(1);

        return $this->succ($info);

    }


    /*
    * 新增/删除/修改
    * */
    public function add()
    {
        if(!isset($this->req['agreement_content']) || empty($this->req['agreement_content'])){
            return $this->fail("协议内容不能为空");
        }

        $where=[];
        $where[] = ["agreement_id",'=',1];
        $res = $this->agreementModel->updateAll($where,[
            "agreement_content"=>$this->req['agreement_content']]
        );

        if($res){
            $this->delCache(1);
            return $this->succ();
        }else{
            return $this->fail();
        }
    }




    public function delCache($id)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete($this->cacheKey.$id);
    }


    public function delAllCache()
    {
        $redisModel = new RedisModel();

        return $redisModel->deleteAll($this->cacheKey);
    }
}
